﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WAContratos.Manager;
using WebContratos.Bean;

namespace WebContratos
{
    public partial class consultaAdendum : System.Web.UI.Page
    {
        public ContratoManager unContra = new ContratoManager();
        public AdendumBean unGaran = new AdendumBean();
        public ArrayList unArray = null;
        public string codContrato = "";
        public string codActa = "";
        public string tipo = "";


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!(IsPostBack))
                {
                    codContrato = Convert.ToString(Session["codContrato"]);
                    tipo = Convert.ToString(Request.QueryString["tipo"]);

                    if (tipo.Equals("actualiza"))
                    {
                        codActa = Convert.ToString(Request.QueryString["codAdendum"]);

                        unContra = new ContratoManager();
                        unGaran = new AdendumBean();

                        unArray = unContra.ObtieneAdendumContra(codContrato, codActa);

                        unGaran = (AdendumBean)unArray[2];

                        this.lblTipo.Text = tipo;

                        this.txtCodAdendum.Text = unGaran.CodigoAdendum;
                        this.txtFechaEntrega.Text = unGaran.FechaSuscripcion;
                        this.txtInformeAprobante.Text = unGaran.InformeAprobante;
                        this.txtInformeSolicitante.Text = unGaran.InformeSolicitante;
                        this.txtObjetoAdendum.Text = unGaran.ObjetoAdendum;
                        this.txtFechaExtension.Text = unGaran.FechaExtension;
                        
                        this.txtCodContrato.Text = codContrato;

                    }
                    else
                    {
                        this.txtCodContrato.Text = codContrato;
                        this.txtCodAdendum.Enabled = true;
                        this.txtCodAdendum.ReadOnly = false;
                    }


                }
            }
            catch (Exception e2)
            {
                MsgBox1.alert(e2.Message);
            }
        }

        protected void btnGrabar_Click(object sender, EventArgs e)
        {
            string sql = "";



            if (this.lblTipo.Text.Equals("actualiza"))
            {
                sql = "update t_adendum " +
                              " set FECHA_SUSCRIPCION = to_date('" + this.txtFechaEntrega.Text + "','rrrr-mm-dd'), " +
                              "     OBJETO_ADENDUM = '" + this.txtObjetoAdendum.Text + "'," +
                              "     INFORME_SOLICITANTE = '" + this.txtInformeSolicitante.Text + "'," +
                              "     INFORME_APROBANTE = '" + this.txtInformeAprobante.Text + "'," +
                              "     FECHA_EXTENSION = to_date('" + this.txtFechaExtension.Text + "','rrrr-mm-dd') " +
                              " where codigo_contrato = '" + this.txtCodContrato.Text + "' and CODIGO_ADENDUM = '" + this.txtCodAdendum.Text + "'";

                cls_datos unDato = new cls_datos();

                bool result = unDato.insertaq(sql);

                /*if (result)
                    MsgBox1.alert("Se Actualizo Correctamente la Garantia No " + this.txtCodGarantia.Text);*/

                string jScript;
                jScript = "<script>consuAdendum('" + this.txtCodContrato.Text + "');</script>";
                //consuGaran('" + this.txtCodContrato.Text + "');</script>";



                //this.codContratoGar.Text = this.txtCodContrato.Text;


                Page.RegisterStartupScript("keyClientBlock", jScript);
            }
            else
            {


                sql = "insert into t_adendum " +
                                     " (   codigo_contrato, " +
                                     "     CODIGO_ADENDUM, " +
                                     "     OBJETO_ADENDUM, " +
                                      "    FECHA_SUSCRIPCION,  " +
                                      "    INFORME_SOLICITANTE, " +
                                      "    INFORME_APROBANTE," +
                                      "    FECHA_EXTENSION " +
                                      ") " +
                                      " values( " +
                                               "'" + this.txtCodContrato.Text + "'," +
                                               "'" + this.txtCodAdendum.Text + "'," +
                                               "'" + this.txtCodAdendum.Text + "'," +
                                               "to_date('" + this.txtFechaEntrega.Text + "','rrrr-mm-dd'), " +
                                               "'" + this.txtInformeSolicitante.Text + "'," +
                                               "'" + this.txtInformeAprobante.Text + "'," +
                                               "to_date('" + this.txtFechaExtension.Text + "','rrrr-mm-dd')" + ")";

                cls_datos unDato = new cls_datos();

                bool result = unDato.insertaq(sql);

                /*if (result)
                    MsgBox1.alert("Se Actualizo Correctamente la Garantia No " + this.txtCodGarantia.Text);*/

                string jScript;
                jScript = "<script>consuAdendum('" + this.txtCodContrato.Text + "');</script>";
                //consuGaran('" + this.txtCodContrato.Text + "');</script>";



                //this.codContratoGar.Text = this.txtCodContrato.Text;


                Page.RegisterStartupScript("keyClientBlock", jScript);
            }
        }
    }
}
